<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/cafe/_init_model_cafe.php';
	
	class mod_cafe_product extends _init_model_cafe{
		public $CAF_PDT_ID;
		public $CAF_PDT_CODE;
		public $CAF_PDT_NAME;
		public $CAF_PDT_PRICE;
		public $CAF_PDT_DC;
		public $CAF_PDT_DESC;
		public $CAF_PDT_IMG;
		public $CAF_PDT_CDATE;
		public $CAF_PDT_UDATE;
		public $CAF_PDT_CAT_ID;
		public $CAF_PDT_VIEWCOUNT;
		public $CAF_ID;
		
		function mod_cafe_product(){
			parent::__construct();
		
			$this->tableName = "CAFE_PRODUCT";
			
			$this->load->library('custom_datetime');
			$this->CAF_PDT_CDATE = $this->custom_datetime->current_date();
			$this->CAF_PDT_UDATE = $this->custom_datetime->current_time();
			
			$this->CAF_PDT_VIEWCOUNT = 0;
		}
			
		function get_single_product($product_code){
			$this->db->where('CAF_PDT_CODE', $product_code);			
			$this->db->join('cafe_product_category', 'cafe_product_category.CAF_PDT_CAT_ID = cafe_product.CAF_PDT_CAT_ID');
			$query = $this->db->get($this->tableName);			
			
			if ($query->num_rows() > 0){
				$this->load->library('define/directory_path');
				
				$row = $query->row_array(0);				
				if (empty($row['CAF_PDT_UDATE']))
					$row['CAF_PDT_UDATE'] = '-';
				if (empty($row['CAF_PDT_CDATE']))
					$row['CAF_PDT_CDATE'] = '-';				
				
				if (empty($row['CAF_PDT_IMG']))					 
					$row['CAF_PDT_IMG'][0] = $this->directory_path->img_no_cafe_product_path;				
				else{
					$img_arr = explode(',', $row['CAF_PDT_IMG']);
					$img_return = array();
					
					for ($i=0; $i<count($img_arr); $i++)
						array_push($img_return, $this->directory_path->img_cafe_product_path . $img_arr[$i]);
						
					$row['CAF_PDT_IMG'] = $img_return;
				}					
				
				return $row;
			} else		
				return null;
		}
		function get_shuffle_product($cafe_id, $limit=5){			
			
			$this->db->select(array('cafe_product.*','cafe_product_category.CAF_PDT_CAT_NAME'));
			$this->db->where('CAF_ID', $cafe_id);
			$this->db->join('cafe_product_category', 'cafe_product_category.CAF_PDT_CAT_ID = cafe_product.CAF_PDT_CAT_ID');
			$this->db->order_by('CAF_ID', 'RANDOM');			
			$query = $this->db->get($this->tableName, $limit);			
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
			
				for ($i=0; $i<count($return); $i++){
							
					if (isset($return[$i]['CAF_PDT_IMG']) && !empty($return[$i]['CAF_PDT_IMG'])){				
						$img_arr = explode(',', $return[$i]['CAF_PDT_IMG']);	
							
						$return[$i]['CAF_PDT_IMG'] = $this->directory_path->img_cafe_product_path . $img_arr[0];				
					} else 
						$return[$i]['CAF_PDT_IMG'] = $this->directory_path->img_no_cafe_product_path;
				}
			}
			
			return $return;
		}
		function get_product_id_from_product_code($product_code){
			$this->db->select(array('CAF_PDT_ID'));
			$this->db->where('CAF_PDT_CODE', $product_code);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_PDT_ID;
			else
				return null;
		}
		function get_product_list($cafe_id=null, $category_id=null, $display_length=null, $start_index=null, $fields=null){
			if (!is_null($fields))
				$this->db->select($fields);
			if (!is_null($cafe_id))
				$this->db->where('cafe_product.CAF_ID', $cafe_id);						
			if (!is_null($category_id))
				$this->db->where('cafe_product.CAF_PDT_CAT_ID', $category_id);
			
			$this->db->order_by('cafe_product.CAF_PDT_ID', 'DESC');
			
			$this->db->join('cafe_product_category', 'cafe_product_category.CAF_PDT_CAT_ID = cafe_product.CAF_PDT_CAT_ID');
			
			if (!is_null($display_length) && !is_null($start_index))
				$query = $this->db->get($this->tableName, $display_length, $start_index);
			else
				$query = $this->db->get($this->tableName);
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				
				for ($i=0; $i<count($return); $i++){
					
					if (isset($return[$i]['CAF_PDT_UDATE'])){
						if (empty($return[$i]['CAF_PDT_UDATE']))
							$return[$i]['CAF_PDT_UDATE'] = '-';
					}
						
					if (isset($return[$i]['CAF_PDT_CDATE'])){
						if (empty($return[$i]['CAF_PDT_CDATE']))
							$return[$i]['CAF_PDT_CDATE'] = '-';
					}
						
					if (isset($return[$i]['CAF_PDT_IMG']) && !empty($return[$i]['CAF_PDT_IMG'])){				
						$img_arr = explode(',', $return[$i]['CAF_PDT_IMG']);	
							
						$return[$i]['CAF_PDT_IMG'] = $this->directory_path->img_cafe_product_path . $img_arr[0];				
					} else 
						$return[$i]['CAF_PDT_IMG'] = $this->directory_path->img_no_cafe_product_path;
				}
			}
			
			return $return;
		}
		function get_count($cafe_id=null, $category_id=null){
			if (is_null($cafe_id) && is_null($category_id))
				return parent::get_count();
			else{
				if (!empty($cafe_id) || !is_null($cafe_id))
					$this->db->where('CAF_ID', $cafe_id);
				if (!empty($category_id) || !is_null($category_id))
					$this->db->where('CAF_PDT_CAT_ID', $category_id);
								
				return $this->db->count_all_results($this->tableName);
			}
		}
		function get_product_filename($product_id, $cafe_id=null)
		{
			$this->db->select(array('CAF_PDT_IMG'));
			$this->db->where('CAF_PDT_ID', $product_id);
			
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);
				
			$query = $this->db->get($this->tableName);
			if ($query->num_rows() > 0)
				return $query->row()->CAF_PDT_IMG;
			else
				return null;
		}
		function get_product_related($cafe_id){
			$return = $this->get_shuffle_product($cafe_id);
				
			if (count($return) > 0)
			{
				$this->load->model('local/data/rate/mod_p_rate_result');
				$this->load->model('local/data/rate/mod_p_rate_record');
		
				for ($i=0; $i<count($return); $i++) {
					$return[$i]['SCORE'] = $this->mod_p_rate_result->get_product_result($return[$i]['CAF_PDT_ID'], $cafe_id);
					$return[$i]['VIEW'] = $this->mod_p_rate_record->get_rate_count($return[$i]['CAF_PDT_ID'], $cafe_id);;
				}
			}
				
			return $return;
		}
				
		function increase_viewcount($product_code, $increase_number=1){
			$curr_view = $this->get_current_viewcount($product_code);							
			
			return $this->CI_update(array('CAF_PDT_VIEWCOUNT' => $curr_view + $increase_number),
									array('CAF_PDT_ID' => $product_code));
		}
			
		function gen_product_code($cafe_id, $category_id){
			$sqlcmd = 'SELECT COUNT(CAF_PDT_ID) AS P_COUNT';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE CAF_ID = ' . $cafe_id;
			$sqlcmd .= ' AND CAF_PDT_CAT_ID = ' . $category_id;

			$this->db->where(array('CAF_ID' => $cafe_id,
									'CAF_PDT_CAT_ID' => $category_id));				
			$last_id = $this->db->count_all_results($this->tableName);			
				
			$cafe_id_arg = '00' . $cafe_id;
			$cat_id_arg = '0' . $category_id;
			$last_id_arg = '0' . ($last_id+1);
				
			return $cafe_id_arg . $cat_id_arg . $last_id_arg;
		}
		
		function add(){
			$data = array('CAF_PDT_CODE' => $this->CAF_PDT_CODE,
							'CAF_PDT_NAME' => $this->CAF_PDT_NAME,
							'CAF_PDT_PRICE' => $this->CAF_PDT_PRICE,
							'CAF_PDT_DC' => $this->CAF_PDT_DC,
							'CAF_PDT_DESC' => $this->CAF_PDT_DESC,
							'CAF_PDT_IMG' => $this->CAF_PDT_IMG,
							'CAF_PDT_CDATE' => $this->CAF_PDT_CDATE,
							'CAF_PDT_CAT_ID' => $this->CAF_PDT_CAT_ID,
							'CAF_PDT_VIEWCOUNT' => $this->CAF_PDT_VIEWCOUNT,
							'CAF_ID' => $this->CAF_ID);
			return $this->db->insert($this->tableName, $data);
		}
		function edit($product_code){
			$this->load->library('custom_datetime');
			$this->CAF_PDT_UDATE = $this->custom_datetime->current_date();
			
			$data = array();
			if (!empty($this->CAF_PDT_NAME))
				$data['CAF_PDT_NAME'] = $this->CAF_PDT_NAME;
			if (!empty($this->CAF_PDT_PRICE))
				$data['CAF_PDT_PRICE'] = $this->CAF_PDT_PRICE;
			if (!empty($this->CAF_PDT_DC))
				$data['CAF_PDT_DC'] = $this->CAF_PDT_DC;
			if (!empty($this->CAF_PDT_DESC))
				$data['CAF_PDT_DESC'] = $this->CAF_PDT_DESC;
			if (!empty($this->CAF_PDT_IMG))
				$data['CAF_PDT_IMG'] = $this->CAF_PDT_IMG;
			if (!empty($this->CAF_PDT_UDATE))
				$data['CAF_PDT_UDATE'] = $this->CAF_PDT_UDATE;
			if (!empty($this->CAF_PDT_CAT_ID))
				$data['CAF_PDT_CAT_ID'] = $this->CAF_PDT_CAT_ID;
			
			if (count($data) > 0) {			
				$this->db->where('CAF_PDT_CODE', $product_code);
				return $this->db->update($this->tableName, $data);
			} else 
				return true;
		}
		function remove_by_product($product){
				
			if (is_array($product)){
				// remove multiple product
				$this->load->model('local/data/rate/mod_p_rate_result');
				if ($this->mod_p_rate_result->remove_by_product_in($product)){
					$this->load->model('local/data/rate/mod_p_rate_record');
						
					if ($this->mod_p_rate_record->remove_by_product_in($product)){
		
						// remove product image file
						$this->load->library('define/directory_path');
						foreach ($product as $item){
							$filename = $this->get_product_filename($item);
							if (!is_null($filename)){
								unlink($this->directory_path->img_upload_cafe_product_path . $filename);
							}
						}
						$this->db->where_in('CAF_PDT_ID', $product);
						return $this->db->delete($this->tableName);
					}
				}
			} else {
				// remove single product
				$this->load->model('local/data/rate/mod_p_rate_result');
				if ($this->mod_p_rate_result->remove_by_product($product)){
		
					$this->load->model('local/data/rate/mod_p_rate_record');
					if ($this->mod_p_rate_record->remove_by_product($product)){
		
						// remove product image file
						$filename = $this->get_product_filename($product);
						if (!is_null($filename)){
							$this->load->library('define/directory_path');
							unlink($this->directory_path->img_upload_cafe_product_path . $filename);
						}
		
						$this->db->where(array('CAF_PDT_ID' => $product));
						return $this->db->delete($this->tableName);
					}
				}
			}
				
			return false;
		}

		function remove($product_code){
			$this->db->where('CAF_PDT_CODE', $product_code);
			return $this->db->delete($this->tableName);
		}
		function remove_in($products){
			if (is_array($products)){
				$this->db->where_in('CAF_PDT_CODE', $products);
				return $this->db->delete($this->tableName);
			} else
				return false;
		}
		function remove_by_cafe($cafe_id){			
			$result = $this->get_product_list($cafe_id, null, null, null, array('CAF_PDT_IMG'));

			$this->load->library('define/directory_path');
			for ($i=0; $i<count($result); $i++){
				unlink($this->directory_path->img_upload_cafe_product_path . $result[$i]['CAF_PDT_IMG']);
			}

			return parent::remove_by_cafe($cafe_id);
		}
	}
?>